package com.example.myapplication11_10;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        BarChart barChart=findViewById(R.id.barchart);
        List<IBarDataSet> s=new ArrayList<>();

        List<BarEntry> bar1 = new ArrayList<>();
        bar1.add(new BarEntry(0, 240f));
        bar1.add(new BarEntry(1, 520f));
        bar1.add(new BarEntry(2, 600f));
        bar1.add(new BarEntry(3, 765f));
        bar1.add(new BarEntry(4, 460f));
        BarDataSet barDataSet1 = new BarDataSet(bar1, "");
        barDataSet1.setValueTextColor(Color.BLUE); // 值的颜色
        barDataSet1.setValueTextSize(15f); // 值的大小
        barDataSet1.setColor(Color.parseColor("#FF00FF")); // 柱子的颜色
        barDataSet1.setLabel("帅哥"); // 设置标签之后，图例的内容默认会以设置的标签显示
        // 设置柱子上数据显示的格式
        s.add(barDataSet1);


        List<BarEntry> bar2 = new ArrayList<>();
        bar2.add(new BarEntry(0.3f, 210f));
        bar2.add(new BarEntry(1.3f, 450f));
        bar2.add(new BarEntry(2.3f, 430f));
        bar2.add(new BarEntry(3.3f, 440f));
        bar2.add(new BarEntry(4.3f, 180f));
        BarDataSet barDataSet2 = new BarDataSet(bar2, "");
        barDataSet2.setValueTextColor(Color.BLUE); // 值的颜色
        barDataSet2.setColor(Color.parseColor("#8B008B"));
        barDataSet2.setLabel("美女");
        s.add(barDataSet2);

        BarData barData = new BarData(s);
        barData.setBarWidth(0.3f); // 设置柱子的宽度
        barChart.setData(barData);
    }
    private BarDataSet getBarDataSet(float[] f,String title){
        List<BarEntry> data = new ArrayList<>();
        for(int x = 0; x < f.length; x++){
            BarEntry barEntry = new BarEntry(x, f[x]);
            data.add(barEntry);
        }
        return new BarDataSet(data, title);
    }
}
